package com.chixing.service.impl;

import com.chixing.mapper.CarMapper;
import com.chixing.pojo.Car;
import com.chixing.service.HotCarService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class HotCarServiceImpl implements HotCarService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ServerResponse getHotCarByConditions() {
        PageHelper.startPage(1,4);
        String hotCarListRedisKey = "hotCarListRedis";
        List<Car> carList = null;

        if(redisTemplate.hasKey(hotCarListRedisKey)){
            carList = (List)redisTemplate.opsForValue().get(hotCarListRedisKey);
        }else {
            PageHelper.startPage(1,4);
            carList = carMapper.selectByExample(null);
            redisTemplate.opsForValue().set(hotCarListRedisKey,carList,3, TimeUnit.DAYS);
        }
        if (carList != null)
            return ServerResponse.getSucccess(carList);
        return ServerResponse.getFail(null);
    }
}